/*
 * Copyright 2007-2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 * 
 * Project: gtinyurl
 */
package org.gnut.tinyurl.portlet.servlet;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.security.Principal;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import javax.portlet.PortalContext;
import javax.portlet.PortletRequest;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletInputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 
 * @author tungt
 * 
 */
public class TinyHttpServletRequest implements HttpServletRequest {
	HttpServletRequest httpServletRequest;

	private String contextPath;

	private String method;

	private String contentType;

	private ServletInputStream inputStream;

	private BufferedReader reader;
	private PortletRequest portletRequest;
	private Map params;
	protected RemoteProvider remoteProvider;
	/**
	 * 
	 * @param httpServletRequest
	 * @param portletRequest
	 * @param params
	 */
	public TinyHttpServletRequest(HttpServletRequest httpServletRequest,
			PortletRequest portletRequest, Map params) {
		this.httpServletRequest = httpServletRequest;
		this.portletRequest = portletRequest;
		this.params = params;
		remoteProvider =  RemoteBuilder.getProvider(portletRequest, httpServletRequest);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.ServletRequest#getAttribute(java.lang.String)
	 */
	public Object getAttribute(String name) {

		return httpServletRequest.getAttribute(name);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.ServletRequest#getAttributeNames()
	 */
	public Enumeration getAttributeNames() {

		return httpServletRequest.getAttributeNames();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.http.HttpServletRequest#getAuthType()
	 */
	public String getAuthType() {

		return this.httpServletRequest.getAuthType();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.ServletRequest#getCharacterEncoding()
	 */
	public String getCharacterEncoding() {

		return httpServletRequest.getCharacterEncoding();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.ServletRequest#getContentLength()
	 */
	public int getContentLength() {

		return httpServletRequest.getContentLength();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.ServletRequest#getContentType()
	 */
	public String getContentType() {
		if (contentType != null)
			return contentType;
		return httpServletRequest.getContentType();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.http.HttpServletRequest#getContextPath()
	 */
	public String getContextPath() {
		if (contextPath != null)
			return contextPath;

		return this.httpServletRequest.getContextPath();

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.http.HttpServletRequest#getCookies()
	 */
	public Cookie[] getCookies() {
		return httpServletRequest.getCookies();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * javax.servlet.http.HttpServletRequest#getDateHeader(java.lang.String)
	 */
	public long getDateHeader(String name) {
		return httpServletRequest.getDateHeader(name);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.http.HttpServletRequest#getHeader(java.lang.String)
	 */
	public String getHeader(String name) {
		return httpServletRequest.getHeader(name);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.http.HttpServletRequest#getHeaderNames()
	 */
	public Enumeration getHeaderNames() {

		return httpServletRequest.getHeaderNames();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.http.HttpServletRequest#getHeaders(java.lang.String)
	 */
	public Enumeration getHeaders(String name) {

		return httpServletRequest.getHeaders(name);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.ServletRequest#getInputStream()
	 */
	public ServletInputStream getInputStream() throws IOException {
		if (inputStream != null)
			return inputStream;
		return httpServletRequest.getInputStream();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.http.HttpServletRequest#getIntHeader(java.lang.String)
	 */
	public int getIntHeader(String name) {

		return httpServletRequest.getIntHeader(name);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.ServletRequest#getLocalAddr()
	 */
	public String getLocalAddr() {

		return httpServletRequest.getLocalAddr();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.ServletRequest#getLocale()
	 */
	public Locale getLocale() {

		return httpServletRequest.getLocale();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.ServletRequest#getLocales()
	 */
	public Enumeration getLocales() {

		return httpServletRequest.getLocales();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.ServletRequest#getLocalName()
	 */
	public String getLocalName() {

		return httpServletRequest.getLocalName();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.ServletRequest#getLocalPort()
	 */
	public int getLocalPort() {

		return httpServletRequest.getLocalPort();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.http.HttpServletRequest#getMethod()
	 */
	public String getMethod() {
		if (method != null)
			return method;
		return httpServletRequest.getMethod();
	}

	/**
	 * 
	 * @param name
	 * @return
	 */
	private String checkParam(String name) {
		if (params != null && params.get(name) != null) {
			String[] paramStrings = (String[]) params.get(name);
			if (paramStrings.length > 0)
				return paramStrings[0];
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.ServletRequest#getParameter(java.lang.String)
	 */
	public String getParameter(String name) {
		String param = checkParam(name);
		if (param != null)
			return param;
		return portletRequest.getParameter(name);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.ServletRequest#getParameterMap()
	 */
	public Map getParameterMap() {
		Map map;
		if (params != null) {
			map = new HashMap();
			map.putAll(portletRequest.getParameterMap());
			map.putAll(params);
			return map;
		}
		return portletRequest.getParameterMap();
	}
	/*
	 * (non-Javadoc)
	 * @see javax.servlet.ServletRequest#getParameterNames()
	 */
	public Enumeration getParameterNames() {
		Map map;
		if (params != null) {
			map = new HashMap();
			map.putAll(portletRequest.getParameterMap());
			map.putAll(params);
			Set set = map.keySet();
			return new EnumerationInternal(set.iterator());
		}
		return portletRequest.getParameterNames();
	}
	/*
	 * 
	 */
	private String[] checkParams(String name) {
		if (params != null && params.get(name) != null) {
			String[] paramStrings = (String[]) params.get(name);
			return paramStrings;
		}
		return null;
	}
	/*
	 * (non-Javadoc)
	 * @see javax.servlet.ServletRequest#getParameterValues(java.lang.String)
	 */
	public String[] getParameterValues(String name) {
		String[] param = checkParams(name);
		if (param != null)
			return param;
		return portletRequest.getParameterValues(name);
	}
	/*
	 * (non-Javadoc)
	 * @see javax.servlet.http.HttpServletRequest#getPathInfo()
	 */
	public String getPathInfo() {

		return httpServletRequest.getPathInfo();
	}
	/*
	 * (non-Javadoc)
	 * @see javax.servlet.http.HttpServletRequest#getPathTranslated()
	 */
	public String getPathTranslated() {

		return httpServletRequest.getPathTranslated();
	}
	/*
	 * (non-Javadoc)
	 * @see javax.servlet.ServletRequest#getProtocol()
	 */
	public String getProtocol() {

		return httpServletRequest.getProtocol();
	}
	/*
	 * (non-Javadoc)
	 * @see javax.servlet.http.HttpServletRequest#getQueryString()
	 */
	public String getQueryString() {

		return httpServletRequest.getQueryString();
	}
	/*
	 * (non-Javadoc)
	 * @see javax.servlet.ServletRequest#getReader()
	 */
	public BufferedReader getReader() throws IOException {
		if (inputStream != null) {
			reader = new BufferedReader(new InputStreamReader(getInputStream()));
			return reader;
		}
		return httpServletRequest.getReader();
	}
	/*
	 * (non-Javadoc)
	 * @see javax.servlet.ServletRequest#getRealPath(java.lang.String)
	 */
	public String getRealPath(String path) {

		return httpServletRequest.getRealPath(path);
	}
	/*
	 * (non-Javadoc)
	 * @see javax.servlet.ServletRequest#getRemoteAddr()
	 */
	public String getRemoteAddr() {
		
		return remoteProvider.getRemoteAddr();
	}
	/*
	 * (non-Javadoc)
	 * @see javax.servlet.ServletRequest#getRemoteHost()
	 */
	public String getRemoteHost() {

		return remoteProvider.getRemoteHost();
	}
	/*
	 * (non-Javadoc)
	 * @see javax.servlet.ServletRequest#getRemotePort()
	 */
	public int getRemotePort() {

		return remoteProvider.getRemotePort();
	}
	/*
	 * (non-Javadoc)
	 * @see javax.servlet.http.HttpServletRequest#getRemoteUser()
	 */
	public String getRemoteUser() {

		return httpServletRequest.getRemoteUser();
	}
	/*
	 * (non-Javadoc)
	 * @see javax.servlet.ServletRequest#getRequestDispatcher(java.lang.String)
	 */
	public RequestDispatcher getRequestDispatcher(String path) {

		return httpServletRequest.getRequestDispatcher(path);
	}
	/*
	 * (non-Javadoc)
	 * @see javax.servlet.http.HttpServletRequest#getRequestedSessionId()
	 */
	public String getRequestedSessionId() {

		return httpServletRequest.getRequestedSessionId();
	}
	/*
	 * (non-Javadoc)
	 * @see javax.servlet.http.HttpServletRequest#getRequestURI()
	 */
	public String getRequestURI() {

		return httpServletRequest.getRequestURI();
	}
	/*
	 * (non-Javadoc)
	 * @see javax.servlet.http.HttpServletRequest#getRequestURL()
	 */
	public StringBuffer getRequestURL() {

		return httpServletRequest.getRequestURL();
	}
	/*
	 * (non-Javadoc)
	 * @see javax.servlet.ServletRequest#getScheme()
	 */
	public String getScheme() {

		return httpServletRequest.getScheme();
	}
	/*
	 * (non-Javadoc)
	 * @see javax.servlet.ServletRequest#getServerName()
	 */
	public String getServerName() {

		return httpServletRequest.getServerName();
	}
	/*
	 * (non-Javadoc)
	 * @see javax.servlet.ServletRequest#getServerPort()
	 */
	public int getServerPort() {

		return httpServletRequest.getServerPort();
	}
	/*
	 * (non-Javadoc)
	 * @see javax.servlet.http.HttpServletRequest#getServletPath()
	 */
	public String getServletPath() {

		return httpServletRequest.getServletPath();
	}
	/*
	 * (non-Javadoc)
	 * @see javax.servlet.http.HttpServletRequest#getSession()
	 */
	public HttpSession getSession() {

		return httpServletRequest.getSession();
	}
	/*
	 * (non-Javadoc)
	 * @see javax.servlet.http.HttpServletRequest#getSession(boolean)
	 */
	public HttpSession getSession(boolean create) {

		return httpServletRequest.getSession(create);
	}
	/*
	 * (non-Javadoc)
	 * @see javax.servlet.http.HttpServletRequest#getUserPrincipal()
	 */
	public Principal getUserPrincipal() {

		return httpServletRequest.getUserPrincipal();
	}
	/*
	 * (non-Javadoc)
	 * @see javax.servlet.http.HttpServletRequest#isRequestedSessionIdFromCookie()
	 */
	public boolean isRequestedSessionIdFromCookie() {

		return httpServletRequest.isRequestedSessionIdFromCookie();
	}
	/*
	 * (non-Javadoc)
	 * @see javax.servlet.http.HttpServletRequest#isRequestedSessionIdFromUrl()
	 */
	public boolean isRequestedSessionIdFromUrl() {

		return httpServletRequest.isRequestedSessionIdFromUrl();
	}
	/*
	 * (non-Javadoc)
	 * @see javax.servlet.http.HttpServletRequest#isRequestedSessionIdFromURL()
	 */
	public boolean isRequestedSessionIdFromURL() {

		return httpServletRequest.isRequestedSessionIdFromURL();
	}
	/*
	 * (non-Javadoc)
	 * @see javax.servlet.http.HttpServletRequest#isRequestedSessionIdValid()
	 */
	public boolean isRequestedSessionIdValid() {

		return httpServletRequest.isRequestedSessionIdValid();
	}
	/*
	 * (non-Javadoc)
	 * @see javax.servlet.ServletRequest#isSecure()
	 */
	public boolean isSecure() {

		return httpServletRequest.isSecure();
	}	
	/*
	 * (non-Javadoc)
	 * @see javax.servlet.http.HttpServletRequest#isUserInRole(java.lang.String)
	 */
	public boolean isUserInRole(String role) {

		return httpServletRequest.isUserInRole(role);
	}
	/*
	 * 
	 * (non-Javadoc)
	 * @see javax.servlet.ServletRequest#removeAttribute(java.lang.String)
	 */
	public void removeAttribute(String name) {
		httpServletRequest.removeAttribute(name);

	}
	/*
	 * (non-Javadoc)
	 * @see javax.servlet.ServletRequest#setAttribute(java.lang.String, java.lang.Object)
	 */
	public void setAttribute(String name, Object o) {
		httpServletRequest.setAttribute(name, o);

	}
	/*
	 * (non-Javadoc)
	 * @see javax.servlet.ServletRequest#setCharacterEncoding(java.lang.String)
	 */
	public void setCharacterEncoding(String env)
			throws UnsupportedEncodingException {
		httpServletRequest.setCharacterEncoding(env);

	}
	/**
	 * 
	 * @param contentType
	 */
	public void setContentType(String contentType) {
		this.contentType = contentType;
	}
	/**
	 * 
	 * @param contextPath
	 */
	public void setContextPath(String contextPath) {
		this.contextPath = contextPath;
	}
	/**
	 * 
	 * @param inputStream
	 */
	public void setInputStream(final InputStream inputStream) {
		this.inputStream = new ServletInputStream() {
			public int read() throws IOException {
				return inputStream.read();
			}

		};
	}
	/**
	 * 
	 * @param method
	 */
	public void setMethod(String method) {
		this.method = method;
	}

}

/**
 * 
 * @author tungt
 * 
 */
class EnumerationInternal implements Enumeration {
	Iterator iterator;

	/**
	 * 
	 * @param iterator
	 */
	public EnumerationInternal(Iterator iterator) {
		this.iterator = iterator;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.util.Enumeration#hasMoreElements()
	 */
	public boolean hasMoreElements() {
		return iterator.hasNext();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.util.Enumeration#nextElement()
	 */
	public Object nextElement() {
		return iterator.next();
	}
}
